package com.monco.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.monco.common.response.ApiResult;
import com.monco.entity.Goods;
import com.monco.entity.GoodsOrder;
import com.monco.entity.User;
import com.monco.service.IGoodsOrderService;
import com.monco.service.IGoodsService;
import com.monco.service.IUserService;
import com.monco.vo.GoodsOrderVo;
import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author com/monco
 * @since 2020-03-11
 */
@Api(tags = "物品订单模块API")
@RestController
@RequestMapping("/goods-order")
public class GoodsOrderController {

    @Resource
    private IGoodsOrderService goodsOrderService;

    @Resource
    private IGoodsService goodsService;

    @Resource
    private IUserService userService;

    /**
     * 新增GoodsOrder方法
     *
     * @param goodsOrderVo
     * @return
     */
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public ApiResult save(@RequestBody GoodsOrderVo goodsOrderVo) {
        GoodsOrder goodsOrder = new GoodsOrder();
        Subject subject = SecurityUtils.getSubject();
        User user = userService.getUserList(new User().setUsername(subject.getPrincipal().toString())).get(0);
        int userScore = user.getUserScore();
        Goods goods = goodsService.getById(goodsOrderVo.getGoodsId());
        goodsOrder.setGoodsName(goods.getGoodsName());
        goodsOrder.setGoodsScore(goods.getGoodsScore());
        goodsOrder.setUsername(user.getUsername());
        goodsOrder.setNickName(user.getNickName());
        goodsOrder.setGoodsId(goods.getId());
        goodsOrder.setGoodsNumber(goodsOrderVo.getGoodsNumber());
        goodsOrder.setUserId(user.getId());
        int score = goods.getGoodsScore() * goodsOrderVo.getGoodsNumber();
        goodsOrder.setTotalScore(score);
        if (userScore < score) {
            return ApiResult.error("积分不足");
        } else {
            userScore = userScore - score;
        }
        user.setUserScore(userScore);
        goodsOrderService.save(goodsOrder);
        userService.updateById(user);
        return ApiResult.ok();
    }

    /**
     * 修改GoodsOrder方法
     *
     * @param goodsOrder
     * @return
     */
    @PutMapping
    public ApiResult update(@RequestBody GoodsOrder goodsOrder) {
        goodsOrderService.updateById(goodsOrder);
        return ApiResult.ok();
    }

    /**
     * 查询列表
     *
     * @param goodsOrder
     * @return
     */
    @GetMapping("list")
    public ApiResult list(GoodsOrder goodsOrder) {
        return ApiResult.ok(goodsOrderService.getGoodsOrderList(goodsOrder));
    }

    /**
     * 查询分页
     *
     * @param current    当前页
     * @param size       一页多少数据
     * @param goodsOrder
     * @return
     */
    @GetMapping("page")
    public ApiResult page(@RequestParam(required = false, defaultValue = "0") long current,
                          @RequestParam(required = false, defaultValue = "10") long size, GoodsOrder goodsOrder) {
        return ApiResult.ok(goodsOrderService.getGoodsOrderPage(new Page<GoodsOrder>().setCurrent(current).setSize(size), goodsOrder));
    }

    /**
     * 删除方法
     *
     * @param id
     * @return
     */
    @DeleteMapping
    public ApiResult delete(@RequestParam Long id) {
        goodsOrderService.removeById(id);
        return ApiResult.ok();
    }

    /**
     * 按照id获取单条记录
     *
     * @param id
     * @return
     */
    @GetMapping
    public ApiResult getOne(@RequestParam Long id) {
        return ApiResult.ok(goodsOrderService.getById(id));
    }
}
